package com.zoweunion.mechanic.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zoweunion.mechanic.util.Orgin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.service.PromotionCouponService;
import com.zoweunion.mechanic.entity.PromotionCouponEntity;


import com.zoweunion.mechanic.model.ResponseBean;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpServletRequest;


/**
 * 优惠券表
 *
 * @author xp
 * @email 17688825585@163.com
 * @date 2020-08-11 13:32:35
 */
@RestController
@RequestMapping("/promotionCoupon")
public class PromotionCouponController extends BaseController {
    @Autowired
    private PromotionCouponService promotionCouponService;

    /**
     * 列表
     */
    @GetMapping("/promotionCouponList")
    public ResponseBean promotionCouponList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        params.put("user_id", getCurrentUser(request).getId());
        PageInfo info = promotionCouponService.queryPage(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 新增
     */
    @PostMapping("/promotionCouponInsertUpdate")
    public ResponseBean promotionCouponInsertUpdate(@RequestBody PromotionCouponEntity params,
                                                    HttpServletRequest request) {
        if (params.getCouponMoney()>params.getReduceMoney()){
            return new ResponseBean(Constants.CODE_ERROR, "优惠券金额不能大于满减金额", "");
        }
        promotionCouponService.promotionCouponInsertUpdate(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 优惠券列表 app
     */
    @GetMapping("/couponList")
    public ResponseBean couponList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        List<PromotionCouponEntity> info = new ArrayList<>();
        if (Orgin.getInteger(params, "login") == 0) {
            info = promotionCouponService.couponListNotLogin(params);
        } else {
            params.put("user_id", getCurrentUser(request).getId());
            info = promotionCouponService.couponList(params);
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 推荐优惠券
     */
    @GetMapping("/couponRecommend")
    public ResponseBean couponRecommend(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("user_id", getCurrentUser(request).getId());
        PromotionCouponEntity entity = promotionCouponService.couponRecommend(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", entity);
    }

}
